// 点击回到顶部
let backTop = document.querySelector(".footer-main>div>a");
backTop.onclick = function (e) {
  e.preventDefault();
  let distans = document.documentElement.scrollTop;
  let distansControl = setInterval(function () {
    distans -= 10;
    document.documentElement.scrollTop = distans;
    if (distans <= 0) {
      clearInterval(distansControl);
    }
  }, 3);
};

// query接受参数
function getUrlParams() {
    let href = location.href;//获取浏览器的地址
    let regExp = /.+\?(.+)/
    let param = regExp.exec(href)//匹配?后面的参数
    if(!param){
        console.log('没有query参数');
        return
    }
    param = param[1]
    let paramArr = param.split("&");//多个参数以"&"切割 ?name=李雷&age=30
    let query = {}//声明一个对象
    paramArr.forEach(item => {
        //['name','李雷']
        let itemArr = item.split("=");
        query[itemArr[0]] = decodeURI(itemArr[1])
    })
    return query
}

let Params = getUrlParams()

const getbrandtitleId = (num = 1) =>
  ajax({
    url: "http://chst.vip:1234/api/getbrandproductlist",
    data: { brandtitleid: num },
  });

//请求数据库
getbrandtitleId(Params.brandtitleid/1)
  .then((res) => {
    bijiaDraw(res)
  })
  
// 渲染三级菜单
let brandName = document.querySelector('#categoryName')
brandName.innerHTML =  Params.brandName

// 渲染主体界面
function bijiaDraw(res) {
  let arr = res.result;
  let html = "";
  arr.forEach((item) => {
    html += `
      <div productid=${item.productId}>
          ${item.productImg}
          <div>
            <p>${item.productName}</p>
            <p>${item.productPrice}</p>
            <p><span>${item.productQuote}</span> <span>${item.productCom}</span></p>
          </div>
       </div>
      `;
  });
  let manman = document.querySelector(".main-main");
  manman.innerHTML = html;
  //创建主体div中的点击事件，并query传参
  [...manman.children].forEach((item) => {
    item.onclick = function () {
      let productid = this.getAttribute("productid");
      location.href = `productlist.html?productid=${productid}&bigName=${categoryName.innerText}`;
    };
  });
}
